package org.autumn.netty;

import cn.hutool.core.io.file.FileReader;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

@Slf4j
public class TestByteBuffer {
    public static void main(String[] args) throws IOException {

        try (FileChannel channel = new FileInputStream(new FileReader("netty.txt").getFile()).getChannel()) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(10);

            while (true) {
                int read = channel.read(byteBuffer);
                if (read == -1){
                    break;
                }
                // switch read model
                byteBuffer.flip();
                while (byteBuffer.hasRemaining()){
                    System.out.println((char) byteBuffer.get());
                }
                // switch write model
                byteBuffer.clear();
            }
        } catch (Exception e) {
            log.info("-->", e);
        }

    }
}
